//=======================================================================================
//= Fixes.cpp
//=======================================================================================
#include "StdAfx.h"

//------------------------------------------------------
//- Start
//------------------------------------------------------
bool ProtocolCore (BYTE protoNum,LPBYTE aRecv,int aLen,int aIndex,DWORD Encrypt,DWORD Serial)
{
	//--------------------------------------------------------------------------------
	Player.SetIndex(aIndex);
	//--------------------------------------------------------------------------------
	OBJECTSTRUCT *gObj = (OBJECTSTRUCT*)OBJECT_POINTER(aIndex);
	OBJECTSTRUCT *ObjTable = (OBJECTSTRUCT*)OBJECT_BASE;
	//--------------------------------------------------------------------------------

	switch(BYTE(protoNum))
	{
	case 0x00:
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		if(COMMAND_NAME("/post"))
		{
			//--------------------------------------------------------------------------------
			if(ReadINI.GetInt("Commands","PostCommandActive",INI_COMMANDS) == 0)
			{
				//--------------------------------------------------------------------------------
				Player.SendTextMsg(1,"This command is disabled!");
				//--------------------------------------------------------------------------------
				return false;
				//--------------------------------------------------------------------------------
			}
			//--------------------------------------------------------------------------------
			if(Player.GetLevel() < ReadINI.GetInt("Commands","PostCommandMinLevel",INI_COMMANDS))
			{
				//--------------------------------------------------------------------------------
				Player.SendTextMsg(1,"You Don't have req level for usage. Min level %d",ReadINI.GetInt("Commands","PostCommandMinLevel",INI_COMMANDS));
				//--------------------------------------------------------------------------------
				return false;
				//--------------------------------------------------------------------------------
			}
			//--------------------------------------------------------------------------------
			if(Player.GetMoney() > ReadINI.GetInt("Commands","PostCommandPrice",INI_COMMANDS))
			{
				//--------------------------------------------------------------------------------
				Player.RemoveMoney(ReadINI.GetInt("Commands","PostCommandPrice",INI_COMMANDS));
				//--------------------------------------------------------------------------------
			}
			else
			{
				//--------------------------------------------------------------------------------
				return false;
				//--------------------------------------------------------------------------------
			}
			//--------------------------------------------------------------------------------
			Player.SendAllTextMsg(1,"[Post] %s: %s",gObj->Name,(char*)COMMAND_PARAMS("/post"));
			//--------------------------------------------------------------------------------
		}
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		else if(COMMAND_NAME("/drop"))
		{
			//--------------------------------------------------------------------------------
			char * Cmd = (char*)COMMAND_PARAMS("/drop");
			//--------------------------------------------------------------------------------
			int ItemType,ItemNr,ItemLevel,ItemSkill,ItemLuck,ItemOpt,ItemExc,ItemAncient;
			//--------------------------------------------------------------------------------
			if(Player.IsGameMaster() == false)
			{
				//--------------------------------------------------------------------------------
				return false;
				//--------------------------------------------------------------------------------
			}
			//--------------------------------------------------------------------------------
			if(Player.CanUseCommand("/drop") == false)
			{
				//--------------------------------------------------------------------------------
				return false;
				//--------------------------------------------------------------------------------
			}
			//--------------------------------------------------------------------------------
			sscanf_s(Cmd,"%d %d %d %d %d %d %d %d",&ItemType,&ItemNr,&ItemLevel,&ItemSkill,&ItemLuck,&ItemOpt,&ItemExc,&ItemAncient);
			//--------------------------------------------------------------------------------
			DWORD Item = ItemType * 512 + ItemNr;
			//--------------------------------------------------------------------------------
			ItemSerialCreateSend(aIndex,gObj->MapNumber,gObj->X,gObj->Y,Item,ItemLevel,0,ItemSkill,ItemLuck,ItemOpt,aIndex,ItemExc,ItemAncient);
			//--------------------------------------------------------------------------------
		}
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		else if(COMMAND_NAME("/reload"))
		{
			//--------------------------------------------------------------------------------
			if(Player.IsGameMaster() == false)
			{
				//--------------------------------------------------------------------------------
				return false;
				//--------------------------------------------------------------------------------
			}
			//--------------------------------------------------------------------------------
			Config.EditMemory();
			Fixes.EditMemory();
			LoadIPBlock();
			NewsSystem.Start();
			GMSystem.Load();
			//--------------------------------------------------------------------------------
			Player.SendTextMsg(1,"[Reload] Server Configuration Reloaded!");
			Player.SendTextMsg(1,"[Reload] Blocked IP Configuration Reloaded!");
			Player.SendTextMsg(1,"[Reload] News Configuration Reloaded!");
			Player.SendTextMsg(1,"[Reload] Fixes Configuration Reloaded!");
			Player.SendTextMsg(1,"[Reload] GM System Configuration Reloaded!");
			Player.SendTextMsg(1,"[Reload] Shop Configuration Reloaded!");
			//--------------------------------------------------------------------------------
		}
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		else if(COMMAND_NAME("/gg"))
		{
			//--------------------------------------------------------------------------------
			if(Player.IsGameMaster() == false)
			{
				//--------------------------------------------------------------------------------
				return false;
				//--------------------------------------------------------------------------------
			}
			//--------------------------------------------------------------------------------
			if(Player.CanUseCommand("/gg") == false)
			{
				//--------------------------------------------------------------------------------
				return false;
				//--------------------------------------------------------------------------------
			}
			//--------------------------------------------------------------------------------
			Player.SendAllTextMsg(0,"[%s] %s",gObj->Name,(char*)COMMAND_PARAMS("/gg"));
			//--------------------------------------------------------------------------------
		}
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		else if(COMMAND_NAME("/info"))
		{
			Player.SendTextMsg(1,"=======================================");
			Player.SendTextMsg(1,"STAR Project:");
			Player.SendTextMsg(1,"Server Version %s",SERVER_VERSION);
			Player.SendTextMsg(1,"=======================================");
		}
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		else if(COMMAND_NAME("/gmove"))
		{
			char * Cmd = (char*)COMMAND_PARAMS("/gmove");
			int Map,X,Y;
			//--------------------------------------------------------------------------------
			if(Player.IsGameMaster() == false)
			{
				//--------------------------------------------------------------------------------
				return false;
				//--------------------------------------------------------------------------------
			}
			//--------------------------------------------------------------------------------
			if(Player.CanUseCommand("/gmove") == false)
			{
				//--------------------------------------------------------------------------------
				return false;
				//--------------------------------------------------------------------------------
			}
			//--------------------------------------------------------------------------------
			sscanf(Cmd,"%d %d %d",&Map, &X,&Y);
			//--------------------------------------------------------------------------------
			Player.Teleport(Map,X,Y);
			//--------------------------------------------------------------------------------
		}
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		else if(COMMAND_NAME("/summon"))
		{
			int MonsterID;
			//--------------------------------------------------------------------------------
			if(Player.IsGameMaster() == false && Player.CanUseCommand("/summon") == false)
			{
				//--------------------------------------------------------------------------------
				return false;
				//--------------------------------------------------------------------------------
			}
			else
			{
				//--------------------------------------------------------------------------------
				sscanf((char*)COMMAND_PARAMS("/summon"),"%d",&MonsterID);
				//--------------------------------------------------------------------------------
				gObjMonsterCallPet(aIndex,MonsterID);
				//--------------------------------------------------------------------------------
			}
			//--------------------------------------------------------------------------------
		}
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		break;
	case 0x03:
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		Player.SendTextMsg(0,ConnectMessage,gObj->Name);
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		if(ReadINI.GetInt("Main","ShowNoticeOnGMEnter",INI_NPC)  == 1)
		{
			if(Player.IsGameMaster() == true)
			{
				Player.SendAllTextMsg(0,"Game Master %s has joined!",gObj->Name);
			} 
		}
		break;
	case 0x30:
		{
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		OBJECTSTRUCT *ObjNPC = (OBJECTSTRUCT*) OBJECT_POINTER ((int)PGetNPC_ID);
		//--------------------------------------------------------------------------------
		if(ObjNPC->Class == ReadINI.GetInt("TeleportNPC","TeleportNPC_ID",INI_NPC) && ObjNPC->MapNumber == ReadINI.GetInt("TeleportNPC","TeleportNPC_MAP",INI_NPC) && ObjNPC->X == ReadINI.GetInt("TeleportNPC","TeleportNPC_X",INI_NPC) && ObjNPC->Y == ReadINI.GetInt("TeleportNPC","TeleportNPC_Y",INI_NPC))
		{
			//--------------------------------------------------------------------------------
			if(ReadINI.GetInt("TeleportNPC","TeleportNPC_GM",INI_NPC) == 1)
			{
				if(Player.IsGameMaster() == false)
				{
					//--------------------------------------------------------------------------------
					return false;
					//--------------------------------------------------------------------------------
				}
			}
			//--------------------------------------------------------------------------------
			if(ReadINI.GetInt("TeleportNPC","TeleportNPC_Active",INI_NPC) == 0)
			{
				//--------------------------------------------------------------------------------
				Player.SendTextMsg(1,"Teleport is disabled!");
				//--------------------------------------------------------------------------------
				return false;
				//--------------------------------------------------------------------------------
			}
			//--------------------------------------------------------------------------------
			if(Player.GetLevel() < ReadINI.GetInt("TeleportNPC","TeleportNPC_MinLevel",INI_NPC))
			{
				//--------------------------------------------------------------------------------
				Player.SendTextMsg(1,"You Don't have req level for usage. Min level %d",ReadINI.GetInt("TeleportNPC","TeleportNPC_MinLevel",INI_NPC));
				//--------------------------------------------------------------------------------
				return false;
				//--------------------------------------------------------------------------------
			}
			//--------------------------------------------------------------------------------
			if(Player.GetMoney() > ReadINI.GetInt("TeleportNPC","TeleportNPC_Price",INI_NPC))
			{
				//--------------------------------------------------------------------------------
				Player.RemoveMoney(ReadINI.GetInt("TeleportNPC","TeleportNPC_Price",INI_NPC));
				//--------------------------------------------------------------------------------
			}
			else
			{
				//--------------------------------------------------------------------------------
				return false;
				//--------------------------------------------------------------------------------
			}
			//--------------------------------------------------------------------------------
			Player.Teleport(ReadINI.GetInt("TeleportNPC","TeleportTo_MAP",INI_NPC),ReadINI.GetInt("TeleportNPC","TeleportTo_X",INI_NPC),ReadINI.GetInt("TeleportNPC","TeleportTo_Y",INI_NPC));
			//--------------------------------------------------------------------------------
			Console.Write("[TeleportNPC][Nick: %s] Player has been teleported!",gObj->Name);
			//--------------------------------------------------------------------------------
		}
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		break;
		}
	case 0xBC:
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		if(ObjTable[aIndex].m_IfState.use > 0 && ObjTable[aIndex].m_IfState.type != 12)
		{
			//--------------------------------------------------------------------------------
			return true;
			//--------------------------------------------------------------------------------
		}
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		break;
	case 0x95:
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		//GoldenArcher
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		break;
	case 0x26:
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		//CGUseItemRecv((PMSG_USEITEM*)aRecv,aIndex);
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		break;
	case 0x17:
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		//CRateSystem.Work(aIndex,gObj->MapNumber);
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		break;
	case 0xF1:
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		if(aRecv[3] == 0x01)
		{
			//--------------------------------------------------------------------------------
			char IPAddr[16];
			Player.GetIP(IPAddr);
			//--------------------------------------------------------------------------------
			for(int x=1;x < IPBlockCount;x++)
			{
				//--------------------------------------------------------------------------------
				if(!strcmp(IPBlockInfo[x].IP,IPAddr))
				{
					//--------------------------------------------------------------------------------
					Player.Disconnect();
					//--------------------------------------------------------------------------------
					return false;
					//--------------------------------------------------------------------------------
				}
				//--------------------------------------------------------------------------------
			}
		}
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		break;
	case 0x18:
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
		if(aLen > 5)
		{
			//--------------------------------------------------------------------------------
			gObjDel(aIndex);	// Fix Large Packet Send to Server, All DC!
			//--------------------------------------------------------------------------------
		}
		//-----------------------------------------------------------------------------------------------------------------------------------------------------
	break;
	}
	//--------------------------------------------------------------------------------
	DataRecv(protoNum,aRecv,aLen,aIndex,Encrypt,Serial);
	//--------------------------------------------------------------------------------
	return true;
	//--------------------------------------------------------------------------------
}

BOOL gObjSetMonsterEx(int aIndex, int MonsterClass)
{
	//--------------------------------------------------------------------------------
	BOOL bValue = gObjSetMonster(aIndex,MonsterClass);
	//--------------------------------------------------------------------------------
	OBJECTSTRUCT *gObj = (OBJECTSTRUCT*)OBJECT_POINTER(aIndex);
	//--------------------------------------------------------------------------------
	if (aIndex < 0 || aIndex > 9000)
	{
		//--------------------------------------------------------------------------------
		return false;
		//--------------------------------------------------------------------------------
	}
	//--------------------------------------------------------------------------------
	bool bCheck = false;
	//--------------------------------------------------------------------------------
	switch ( MonsterClass )
	{
		//--------------------------------------------------------------------------------
		case 258:						// Luke The Helper
			gObj->Type=3;				// Set Monster Type to NPC
			gObj->m_AttackType = -1;	// Disable Attact to NPC
			bCheck=true;
			break;
		//--------------------------------------------------------------------------------
		case 371:						// Leo The Helper
			gObj->Type=3;				// Set Monster Type to NPC
			gObj->m_AttackType = -1;	// Disable Attact to NPC
			bCheck=true;
			break;
		//--------------------------------------------------------------------------------
	}
	//--------------------------------------------------------------------------------
	if (bCheck==false)
	{
		//--------------------------------------------------------------------------------
		return bValue;
		//--------------------------------------------------------------------------------
	}
	else
	{
		//--------------------------------------------------------------------------------
		return true;
		//--------------------------------------------------------------------------------
	}
	//--------------------------------------------------------------------------------
}